package com.hzqy.service.virtualchannel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.hzqy.persistence.dao.workOrders.WorkOrderDao;
import com.hzqy.persistence.dao.picture.PictureDao;
import com.hzqy.persistence.dao.virtualchannel.VirtualChannelDao;
import com.hzqy.service.pms.PmsServiceBean;
import com.hzqy.web.vo.PmsOrgVo;
import com.hzqy.web.vo.PmsUserVo;
import com.hzqy.web.vo.virtualchannel.VirtualChannelMaterialVo;
import com.hzqy.web.vo.virtualchannel.VirtualChannelTemplateVo;
import com.hzqy.web.vo.virtualchannel.VirtualChannelVo;

@Service
public class VirtualChannelServiceBean {
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	
	@Resource
	private VirtualChannelDao virtualChannelDao;
	@Resource
	private WorkOrderDao workOrderDao;
	@Resource
	private PictureDao pictureDao;
	@Resource
	private PmsServiceBean pmsServiceBean;
	
	
	public Map<String,Object> insertTemplate(VirtualChannelTemplateVo virtualChannelTemplateVo){
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = 0;
		if(virtualChannelTemplateVo==null || virtualChannelTemplateVo.getF_vct_name()==null) {
			flag = -1;
		}
		if(flag ==0) {
			VirtualChannelVo vo = new VirtualChannelVo();
			vo.setF_vc_name(virtualChannelTemplateVo.getF_vct_name());//默认为模板名称
			vo.setF_vc_customid(virtualChannelTemplateVo.getF_vct_customid());
			vo.setF_vc_type(1);//
			vo.setF_vc_status(1);
			virtualChannelDao.insert_virtual_channel(vo);
			
			int f_vc_id = vo.getF_vc_id();
			virtualChannelTemplateVo.setF_vct_channelid(f_vc_id);
			flag = virtualChannelDao.insertTemplate(virtualChannelTemplateVo);
			vo.setF_vc_parentid(f_vc_id);
			virtualChannelDao.update_virtual_channel(vo);
			resultMap.put("success", true);
			resultMap.put("result","新增成功!");
		}else{
			resultMap.put("success", false);
			resultMap.put("result","新增失败!");
		}
		return resultMap;
	}
	/**
	 * 列表获取
	 * @param userVo 
	 * @param VirtualChannelTemplateVo
	 * @param curPage
	 * @param pageSize
	 * @return
	 */
	public Map<String,Object> selectTemplateList(PmsUserVo userVo, VirtualChannelTemplateVo virtualChannelTemplateVo, int curPage,int pageSize) {
		Map<String,Object> map = new HashMap<String, Object>();
		if(userVo.getParentOrgId() > 0){
			String orgIds = pmsServiceBean.getIndustryAuthorityPmsOrgIds(userVo);
			virtualChannelTemplateVo.setOrgIds(orgIds);
			map.put("orgIds",orgIds);
		}
		
		Integer rowCount = virtualChannelDao.findTemplateCount(virtualChannelTemplateVo);
		map.put("total", rowCount);
		if(rowCount > 0 && curPage > 0 && pageSize > 0) {
			map.put("firstResult", (curPage-1)*pageSize);
			map.put("maxResults", pageSize);
			map.put("f_vct_channelid", virtualChannelTemplateVo.getF_vct_channelid());
			map.put("f_vct_status", virtualChannelTemplateVo.getF_vct_status());
			map.put("f_vct_customid", virtualChannelTemplateVo.getF_vct_customid());
			map.put("f_vct_name", virtualChannelTemplateVo.getF_vct_name());
			List<VirtualChannelTemplateVo> results = virtualChannelDao.findTemplateList(map);
			map.put("rows", results);
		} else {
			map.put("rows", new ArrayList<VirtualChannelTemplateVo>());
		}
		return map;
	}
	
	
	
	public VirtualChannelTemplateVo selectTemplate(VirtualChannelTemplateVo virtualChannelTemplateVo, PmsUserVo userVo) {
		if(userVo.getParentOrgId() > 0){
			String orgIds = pmsServiceBean.getIndustryAuthorityPmsOrgIds(userVo);
			virtualChannelTemplateVo.setF_vct_customids(orgIds);
		}
		return  virtualChannelDao.selectTemplate(virtualChannelTemplateVo);
	}
	public int updateTemplate(VirtualChannelTemplateVo virtualChannelTemplateVo) {
		return  virtualChannelDao.updateTemplate(virtualChannelTemplateVo);
	}
	public int deleteTemplate(int f_vct_id) {
		return virtualChannelDao.deleteTemplate(f_vct_id);
	}
	
	/**
	 * 修改节目单相关信息，包括节目单时段名称，该节目单的开播时间和停播时间，和该节目单对应的素材列表
	 * 其中插播的节目单修改也是调用此方法。
	 * @param userSaveVo 用户提交的要修改的vo
	 * @return
	 */
	public Map<String, Object> updateVirtualChannelMaterial(VirtualChannelVo userSaveVo){
		Map<String,Object> resultMap = new HashMap<String, Object>();
		int flag=0;
		Map<String,String> validateMap = new HashMap<String, String>();
		List<VirtualChannelVo> list = new ArrayList<VirtualChannelVo>();
		if(userSaveVo.getF_vc_type()==2){//插播节目单修改
			//Map<String, Object> infoParamMap = new HashMap<String, Object>();
			//infoParamMap.put("f_vc_id", userSaveVo.getF_vc_id());
			//在修改插播的时候，该插播节目单对应的唯一id和该节目单对应的父id都是可以从页面上传过来的。
			//插播的修改按钮 是在节目单列表页点击修改进入修改页后才看到的，插播节目单的唯一id和其父id都是可以在该节目单对应的插播节目单列表中得到，
			//插播节目单的父id也可以直接引用节目单列表页点修改的那条数据的唯一id
			Map<String, Object> vcParamMap = new HashMap<String, Object>();
			vcParamMap.put("f_vc_parentid", userSaveVo.getF_vc_parentid());
			vcParamMap.put("f_vc_type", userSaveVo.getF_vc_type());
			list = virtualChannelDao.selectVirtualChannel(vcParamMap);//虚拟频道的节目单列表
			LOG.debug("修改插播时对应的唯一id:"+userSaveVo.getF_vc_id()+";对应的节目单id:"+userSaveVo.getF_vc_parentid()+";对应的时段名称:"+userSaveVo.getF_vc_name()+";已存在的插播节目单个数:"+list.size());
			validateMap = VirtualChannelVoValidation.validationValue(userSaveVo, list);
		}else{//非插播节目单信息修改
			Map<String, Object> vcParamMap = new HashMap<String, Object>();
			vcParamMap.put("f_vc_parentid", userSaveVo.getF_vc_parentid());
			vcParamMap.put("f_vc_type", userSaveVo.getF_vc_type());
			list = virtualChannelDao.selectVirtualChannel(vcParamMap);//虚拟频道的节目单列表
			validateMap = VirtualChannelVoValidation.validationValue(userSaveVo, list);
		}
		
		
		if("false".equals(validateMap.get("flag"))){//开播时间和停播时间验证不通过
			LOG.debug("修改开播时间和停播时间验证不通过");
			resultMap.put("flag", validateMap.get("flag"));
			resultMap.put("success", false);
			resultMap.put("result",validateMap.get("message"));
			return resultMap;
		}
		
		flag = virtualChannelDao.updateChannel(userSaveVo);
		
		resultMap.put("f_vcm_channelid",userSaveVo.getF_vc_id());
		List<VirtualChannelMaterialVo> results =null;
		results  = virtualChannelDao.selectMaterial(resultMap);
		
		if(results!=null){//素材状态=》伪删除
			for(int i=0;i<results.size();i++){
				VirtualChannelMaterialVo vcm =new VirtualChannelMaterialVo();
				vcm.setF_vcm_id(results.get(i).getF_vcm_id());
				vcm.setF_vcm_status(3);
				flag = virtualChannelDao.update_material(vcm);
			}
		}
		
		List<VirtualChannelMaterialVo> vcmList = userSaveVo.getVirtualChannelMaterialVos();
		if(vcmList.size()>0){//素材新增入库 virtual_channel_material
			for(int i=0;i<vcmList.size();i++){
				VirtualChannelMaterialVo vcmVo = vcmList.get(i);
				LOG.debug("getF_vcm_materialName="+vcmVo.getF_vcm_materialName());
				if(vcmVo.getF_vcm_materialName() == null){
					continue;
				}
				vcmVo.setF_vcm_status(1);
				vcmVo.setF_vcm_channelid(userSaveVo.getF_vc_id());
				virtualChannelDao.insert_vchannel_material(vcmVo);
			}
		}
		if(flag>0){
			resultMap.put("flag", "true");
			resultMap.put("message", "修改成功");
		}else{
			resultMap.put("flag", "false");
			resultMap.put("message", "修改失败");
		}
		return resultMap; 
	}
	public VirtualChannelVo selectVirtualChannelMaterialById(int f_vcm_channelid,PmsUserVo userVo){
		Map<String,Object> map = new HashMap<String,Object>();
		if(userVo.getParentOrgId() > 0){
			String orgIds = pmsServiceBean.getIndustryAuthorityPmsOrgIds(userVo);
			map.put("orgIds",orgIds);
		}
		map.put("f_vcm_channelid",f_vcm_channelid);
		VirtualChannelVo virtualChannelVo = virtualChannelDao.selectVirtualChannelByFvcId(map);
		List<VirtualChannelMaterialVo>  virtualChannelMaterialVoList = virtualChannelDao.selectVirtualChannelMaterialById(f_vcm_channelid);
		LOG.debug("virtualChannelMaterialVoList.size()="+virtualChannelMaterialVoList.size());
		if(virtualChannelMaterialVoList.size()>0){
			virtualChannelVo.setVirtualChannelMaterialVos(virtualChannelMaterialVoList);
		}
		return virtualChannelVo;
	}
	
	/**
	 * 查询一个虚拟频道对应的节目单列表，会left join 查询素材表，在展示节目单列表的时候是需要把素材名称也显示出来的
	 * @param virtualChannelVo
	 * @return
	 */
	public List<VirtualChannelVo> selectChannelList(VirtualChannelVo virtualChannelVo, PmsUserVo userVo) {
		Map<String,Object> map = new HashMap<String, Object>();
		if(userVo.getParentOrgId() > 0){
			String orgIds = pmsServiceBean.getIndustryAuthorityPmsOrgIds(userVo);
			LOG.debug("orgIds:"+orgIds);
			map.put("orgIds",orgIds);
		}
		map.put("f_vc_id", virtualChannelVo.getF_vc_id());
		map.put("f_vc_airTime", virtualChannelVo.getF_vc_airTime());
		map.put("f_vc_offAirTime", virtualChannelVo.getF_vc_offAirTime());
		map.put("f_vc_parentid", virtualChannelVo.getF_vc_parentid());
		
		//`f_vc_type` int(10) DEFAULT NULL COMMENT '类型，1，正常编辑节目 2，插播节目',
		map.put("f_vc_type", virtualChannelVo.getF_vc_type());//查询节目单列表时只查询正常编辑节目，插播的节目在预览的时候显示
		map.put("f_vc_customid", virtualChannelVo.getF_vc_customid());//客户id
		map.put("f_vc_status", virtualChannelVo.getF_vc_status());// 规定状态为5的为现在在投放的节目单
		map.put("f_vc_putInDate", virtualChannelVo.getF_vc_putInDate());//投放日期 最近一次的投放期
		//节目单列表只能显示一个节目单列表 也就是f_vc_parentid这个值只能有一个，多个是在模板那里显示的
		//f_vc_putInDate，f_vc_status这两个参数确定一条记录
		
		List<VirtualChannelVo> results =null;
		results  = virtualChannelDao.selectChannelList( map);
		return results;
	}
	
	/**
	 * 查询一个节目单详情
	 * 在一个节目单下面可能有插播的节目，插播的节目现在的设计方式：把插播的节目当成一个节目单，节目单的子节目单
	 * @param virtualChannelVo
	 * @return
	 */
	public VirtualChannelVo selectVirtualChannel(VirtualChannelVo vo,PmsUserVo userVo) {
		Map<String,Object> map = new HashMap<String, Object>();
		if(userVo.getParentOrgId() > 0){
			String orgIds = pmsServiceBean.getIndustryAuthorityPmsOrgIds(userVo);
			map.put("orgIds",orgIds);
		}
		//以下是查询该节目单下的插播节目单信息，如果是插播修改，有f_vc_type这个值的限定 ，vcvolist 就有可能为空
		map.put("f_vc_id", vo.getF_vc_id());
		map.put("f_vc_status", vo.getF_vc_status());
		map.put("f_vc_type", vo.getF_vc_type());//f_vc_type 类型，1，正常编辑节目 2，插播节目
		
		//
		map.put("f_vc_customid", vo.getF_vc_customid());
		map.put("f_vc_airTime", vo.getF_vc_airTime());
		map.put("f_vc_offAirTime", vo.getF_vc_offAirTime());
		map.put("f_vc_putInDate", vo.getF_vc_putInDate());
		map.put("f_vc_parentid", vo.getF_vc_parentid());
		
		//根据虚拟频道唯一id查询一下 virtual_channel 表的信息 
		List<VirtualChannelVo> vcvolist  = virtualChannelDao.selectVirtualChannel(map);
		
		if(vcvolist!=null&& vcvolist.size()>0){
			vo = vcvolist.get(0);
			//查询插播列表
			Map<String,Object> childmap = new HashMap<String, Object>();
			childmap.put("f_vc_parentid", vo.getF_vc_id());
			childmap.put("f_vc_type", 2);//f_vc_type 类型，1，正常编辑节目 2，插播节目
			childmap.put("f_vc_customid", vo.getF_vc_customid());
			childmap.put("f_vc_putInDate", vo.getF_vc_putInDate());
			childmap.put("f_vc_status", vo.getF_vc_status());
			//查询节目单下的子节目单(查询插播的节目，插播的节目可能会有多次插播)
			List<VirtualChannelVo> childVc = virtualChannelDao.selectVirtualChannel(childmap);
			for(VirtualChannelVo vv : childVc){
				VirtualChannelMaterialVo paramVo = new VirtualChannelMaterialVo();
				paramVo.setF_vcm_channelid(vv.getF_vc_id());
				//插播节目单素材列表
				vv.setVirtualChannelMaterialVos(selectMaterial(paramVo));
			}
			//插播节目单列表
			vo.setInterCutvirtualChannelVos(childVc);
		}
		
		Map<String,Object> materialParamMap = new HashMap<String, Object>();
		materialParamMap.put("f_vcm_channelid", vo.getF_vc_id());
		materialParamMap.put("f_vcm_status",1);
		List<VirtualChannelMaterialVo> results =virtualChannelDao.selectMaterial(materialParamMap);
		vo.setVirtualChannelMaterialVos(results);//该节目单下的所有素材（正常节目和插播节目单的素材查询方式都是一样的，根据虚拟频道表唯一id进行查询）
		
		return vo;
	}
	
	/**
	 * 查询一个节目单下的所有素材
	 * @param virtualChannelMaterialVo
	 * @return
	 */
	public List<VirtualChannelMaterialVo> selectMaterial(VirtualChannelMaterialVo virtualChannelMaterialVo) {
		Map<String,Object> map = new HashMap<String, Object>();
		//map.put("f_vcm_id",virtualChannelMaterialVo.getF_vcm_id());
		map.put("f_vcm_channelid", virtualChannelMaterialVo.getF_vcm_channelid());
		map.put("f_vcm_status", virtualChannelMaterialVo.getF_vcm_status());
		List<VirtualChannelMaterialVo> results =null;
		results  = virtualChannelDao.selectMaterial(map);
		return results;
	}
	
	public int deleteChannel(int f_vc_id) {
		return virtualChannelDao.deleteChannel(f_vc_id);
	}
	
	/**
	 * 新增节目单，
	 * 节目单的新增和素材的新增是在同一个页面的
	 * @param userVo 
	 * @param virtualChannelVo
	 * @return
	 */
	public Map<String, Object> insert_vchannel_material(PmsUserVo userVo, VirtualChannelVo userSaveVo){
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = 0;
		if(userSaveVo==null || userSaveVo.getF_vc_name()==null) {
			flag = -1;
		}
		if(userSaveVo.getF_vc_customid()==0){
			userSaveVo.setF_vc_customid(userVo.getOrgId());
			LOG.debug("用户没有选择组织，默认该登录用户新增，该用户对应的组织id:"+userVo.getOrgId());
		}
		Map<String,String> validateMap = VirtualChannelVoValidation.checkValue(userSaveVo);
		if("-1".equals(validateMap.get("flag"))){
			resultMap.put("flag", validateMap.get("flag"));
			resultMap.put("success", false);
			resultMap.put("result",validateMap.get("message"));
			return resultMap;
		}
		userSaveVo.setF_vc_status(1);
		if(userSaveVo.getF_vc_offAirTime()==null ||"".equals(userSaveVo.getF_vc_offAirTime())){
			userSaveVo.setF_vc_offAirTime("24:00:00");
		}
		
		boolean firstInsert = true;
		if(userSaveVo.getF_vc_parentid()!=0){//不是第一次新增，2，插播的时候这个id也不等于0
			firstInsert = false;
			//不是第一次新增，判断此次添加节目的时间段是否有效，验证下此次添加节目的时间段是否已经存在其他节目正在播放
			//如果有，提醒用户本时间段已有其他节目，请调整节目播出时间
			if(userSaveVo.getF_vc_type() == 2){
				LOG.debug("插播新增:"+userSaveVo.getF_vc_parentid());
				//被插播节目的详细信息，得到该被插播节目的父id，也就是虚拟频道的id，要根据虚拟频道id查询出该虚拟频道对应的节目单列表
				//新增插播，插播的父id就是虚拟频道节目单列表中一个节目的唯一id，根据父id查询出节目的信息获取该节目的父id，然后根据父id查询对应的节目列表
				Map<String, Object> infoParamMap = new HashMap<String, Object>();
				//infoParamMap.put("f_vc_id", userSaveVo.getF_vc_parentid());
				//List<VirtualChannelVo> tmpList = virtualChannelDao.selectVirtualChannel(infoParamMap);
				//if(tmpList!=null){
					//VirtualChannelVo tmpVo = tmpList.get(0);//如果唯一id确定的时候，只会有一条记录
					Map<String, Object> vcParamMap = new HashMap<String, Object>();
					vcParamMap.put("f_vc_parentid", userSaveVo.getF_vc_parentid());
					vcParamMap.put("f_vc_type", userSaveVo.getF_vc_type());
					List<VirtualChannelVo> tmpList = virtualChannelDao.selectVirtualChannel(vcParamMap);//虚拟频道的节目单列表
					validateMap = VirtualChannelVoValidation.validationValue(userSaveVo, tmpList);//验证结果
				//}
			}else{
				Map<String, Object> vcParamMap = new HashMap<String, Object>();
				vcParamMap.put("f_vc_parentid", userSaveVo.getF_vc_parentid());
				List<VirtualChannelVo> tmpList = virtualChannelDao.selectVirtualChannel(vcParamMap);//虚拟频道的节目单列表
				validateMap = VirtualChannelVoValidation.validationValue(userSaveVo, tmpList);//验证结果
				LOG.debug("非插播节目单新增:"+userSaveVo.getF_vc_parentid()+";falg:"+validateMap.get("flag"));
			}
			
			if("false".equals(validateMap.get("flag"))){//开播时间和停播时间验证不通过
				resultMap.put("flag", validateMap.get("flag"));
				resultMap.put("success", false);
				resultMap.put("result",validateMap.get("message"));
				return resultMap;
			}
		}
		LOG.debug("新增节目单-name:"+userSaveVo.getF_vc_name()+";airtime:"+userSaveVo.getF_vc_airTime()+";offtime:"+userSaveVo.getF_vc_offAirTime()
				+";customid:"+userSaveVo.getF_vc_customid()+";parentid:"+userSaveVo.getF_vc_parentid());
		flag = virtualChannelDao.insert_virtual_channel(userSaveVo);
		
		if(firstInsert){//第一次新增，成功之后把该记录对应的id修改为第一次新增的id
			userSaveVo.setF_vc_parentid(userSaveVo.getF_vc_id());
			virtualChannelDao.update_virtual_channel(userSaveVo);
		}
		
		List<VirtualChannelMaterialVo> vcmList = userSaveVo.getVirtualChannelMaterialVos();
		if(vcmList.size()>0){//素材新增入库 virtual_channel_material
			for(int i=0;i<vcmList.size();i++){
				VirtualChannelMaterialVo vcmVo = vcmList.get(i);
				if(vcmVo.getF_vcm_materialName() == null){
					continue;
				}
				vcmVo.setF_vcm_channelid(userSaveVo.getF_vc_id());
				vcmVo.setF_vcm_status(1);
				virtualChannelDao.insert_vchannel_material(vcmVo);
			}
			
		}
		
		if(flag > 0) {
			resultMap.put("flag", flag);
			resultMap.put("success", true);
			resultMap.put("result","新增成功!");
		}else{
			resultMap.put("flag", -1);
			resultMap.put("success", false);
			resultMap.put("result","新增失败!");
		}
		return resultMap;
	}
	
	public List<PmsOrgVo> pmsOrgList(PmsOrgVo pmsOrgVo) {
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("f_po_name", pmsOrgVo.getF_po_name());
		map.put("f_po_code", pmsOrgVo.getF_po_code());
		List<PmsOrgVo> results =null;
		results  = virtualChannelDao.pmsOrgList( map);
		return results;
	}
	/**
	 * 
	 * @param virtualChannelVo
	 * @return
	 */
	public Map<String, Object> update_effect_status(VirtualChannelVo virtualChannelVo) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = 0;
		virtualChannelVo.setF_vc_status(6);//取消投放
		flag = virtualChannelDao.update_uneffect_status(virtualChannelVo);
		
		virtualChannelVo.setF_vc_status(5);//投放
		flag = virtualChannelDao.update_effect_status(virtualChannelVo);
		if(flag > 0) {
			resultMap.put("flag", flag);
			resultMap.put("success", true);
			resultMap.put("result","操作成功!");
		}else{
			resultMap.put("flag", -1);
			resultMap.put("success", false);
			resultMap.put("result","操作失败!");
		}
		return resultMap;
	}
	public Map<String, Object> update_vchannel_status(VirtualChannelVo vo) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		int flag = 0;
		flag = virtualChannelDao.update_vchannel_status(vo);
		if(flag > 0) {
			resultMap.put("flag", flag);
			resultMap.put("success", true);
			resultMap.put("result","操作成功!");
		}else{
			resultMap.put("flag", -1);
			resultMap.put("success", false);
			resultMap.put("result","操作失败!");
		}
		return resultMap;
	}
}
